require "Common/define"
require "Logic/Config"
require "Common/functions"

---@class DivineInspirationManager
DivineInspirationManager = DivineInspirationManager or {}
local this = DivineInspirationManager
local taskCfg = {}
local taskLabelMap = {}
local costCfg = {}
local rankCfg = {}
local rankSrvInfo = {}
local lableCfg = {}
function DivineInspirationManager.init()
    taskCfg = dataTable.getDataTable("t_holybooktask")
    costCfg = dataTable.getDataTable("t_holybookconsumption")
    rankCfg = dataTable.getDataTable("t_holybookrank")
    lableCfg = dataTable.getDataTable("t_holybookschedule")
end

----设置已领取的章节奖励id(对应t_holybookschedule的f_id)
--function DivineInspirationManager.SetReceivedChapter(receivedList)
--    this.receivedAwardList = receivedList
--end
--
--function DivineInspirationManager.GetReceivedChapter()
--    return this.receivedAwardList or {}
--end

--设置宝箱的领奖情况(对应t_holybookschedule的f_id)
function DivineInspirationManager.SetBoxReceivedList(boxRewardList)
    this.boxRewardList = boxRewardList
end

function DivineInspirationManager.GetBoxReceivedList()
    return this.boxRewardList
end

function DivineInspirationManager.GetBoxCfgData()
    return lableCfg
end

--章节奖励是否已领取
function DivineInspirationManager.CheckHaveChapterAwardReceived(id)
    local receivedList = this.GetReceivedChapter()
    return table.contains(receivedList, id)
end

--每个标签页下的任务完成数量情况
function DivineInspirationManager.SetCompleteMap(typeCompleteMap)
    this.typeCompleteMap = typeCompleteMap
end

function DivineInspirationManager.GetCompleteMap()
    return this.typeCompleteMap
end

function DivineInspirationManager.SetMyRankScore(score)
    this.myRankScore = score
end

function DivineInspirationManager.GetMyRankScore()
    return this.myRankScore or 0
end

function DivineInspirationManager.SetMyTaskScore(score)
    this.myTaskScore = score
end

function DivineInspirationManager.GetMyTaskScore()
    return this.myTaskScore or 0
end

function DivineInspirationManager.SetTaskList(taskList)
    taskLabelMap = {}
    for k,v in pairs(taskList) do
        local onetaskData = {}
        local oneTaskCfg = this.GetOneTaskCfg(k)
        onetaskData.cfgData = oneTaskCfg
        onetaskData.srvData = v
        onetaskData.srvData.status = 0  --0，已领取 1，未完成 2，可领取
        if oneTaskCfg then
            if tonumber(tostring(v.process)) >= tonumber(oneTaskCfg.f_Progress) then
                if not v.rewarded then
                    onetaskData.srvData.status = 2
                end
            else
                onetaskData.srvData.status = 1
            end
            if not taskLabelMap[oneTaskCfg.f_LabelType] then
                taskLabelMap[oneTaskCfg.f_LabelType] = {}
            end
            table.insert(taskLabelMap[oneTaskCfg.f_LabelType], onetaskData)
        else
            printError("找不到任务id"..k)
        end
    end
    for k,v in pairs(taskLabelMap) do
        table.sort(v,function(a, b)
            if a.srvData.status ~= b.srvData.status then
                return a.srvData.status > b.srvData.status
            elseif a.cfgData.f_HolyBookTab ~= b.cfgData.f_HolyBookTab then
                return a.cfgData.f_HolyBookTab < b.cfgData.f_HolyBookTab
            else
                return a.cfgData.f_HolyBookTaskId < b.cfgData.f_HolyBookTaskId
            end
        end)
    end
end

function DivineInspirationManager.GetOneTaskCfg(taskId)
    return taskCfg[tostring(taskId)]
end

function DivineInspirationManager.GetTaskList(label)
    if label then
        return taskLabelMap[label]
    else
        return taskLabelMap
    end
end

--返回完成任务数,总任务数和已领取任务数
function DivineInspirationManager.SetLabelTasksState(label)
    for k,v in pairs(lableCfg) do
        v.status = 0    -- 0-未完成 1-可领取 2-已领取
        if v.f_labelType == label then
            for m,n in pairs(this.typeCompleteMap) do
                if tonumber(m) == label and n >= v.f_tasknumber then
                    v.status = 1
                    if table.contains(this.receivedAwardList, v.f_id) then
                        v.status = 2
                    end
                end
            end
        end
    end
end

--返回完成任务数,总任务数和已领取任务数
function DivineInspirationManager.GetNeedShowLableAwardInfo(label)
    this.SetLabelTasksState(label)
    local id = 99999
    for k,v in pairs(lableCfg) do
        if v.f_labelType == label then
            if v.status == 0 then
                id = math.min(id,v.f_id)
            elseif v.status == 1 then
                id = v.f_id
                break
            end
        end
    end
    if id == 99999 then
        id = this.GetMaxLabelId(label)
    end
    local completeCount = 0
    for k,v in pairs(this.typeCompleteMap) do
        if tonumber(k) == label then
            completeCount = v
        end
    end
    return id, completeCount, lableCfg[tostring(id)].status
end

function DivineInspirationManager.GetMaxLabelId(label)
    local maxId = 0
    for k,v in pairs(lableCfg) do
        if v.f_labelType == label then
            maxId = math.max(maxId, v.f_id)
        end
    end
    return maxId
end

function DivineInspirationManager.GetNeedShowLableCfg(id)
    return lableCfg[tostring(id)]
end

function DivineInspirationManager.SetCostType(type)
    this.type = type
end

function DivineInspirationManager.GetCostType()
    return this.type or 1
end

--活动开启时玩家所在礼包id(对应t_holybookconsumption的f_ChapterId)
function DivineInspirationManager.SetCostChaperId(chapterId)
    this.costChapterId = chapterId
end

function DivineInspirationManager.GetCostChapterId()
    return this.costChapterId or 1
end

--通过赛季id找到对应的集市物品列表
function DivineInspirationManager.GetCostItemList(type)
    local type = type or this.GetCostType()
    local giftChapterId = this.GetCostChapterId()
    local itemList = {}
    for k,v in pairs(costCfg) do
        if giftChapterId >= v.f_ChapterId[1] and giftChapterId <= v.f_ChapterId[2] and v.f_Type == type then
            table.insert(itemList, v)
        end
    end
    table.sort(itemList,function(a,b) return a.f_Order < b.f_Order end)
    return itemList
end

function DivineInspirationManager.CheckCostTabShowRed(activeId, type)
    local costItemData = this.GetCostItemList(type)
    local modelTableData, serverData = logicMgr.ActiveManager.getActiveData(activeId)
    for k,v in pairs(costItemData) do
        if v.f_Price == 0 and serverData.hasBuyMap[tostring(v.f_ItemId)] <= 0 then
            return true
        end
    end
end

--通过赛季id和排名找到对应的排名配置信息
function DivineInspirationManager.GetRankCfgByRank(rank)
    for k,v in pairs(rankCfg) do
        local section = v.f_Rank
        if rank >= section[1] and rank <= section[2] then
            return v
        end
    end
end

function DivineInspirationManager.SetRankSrvInfo(rankData)
    rankSrvInfo[rankData.type] = {}
    rankSrvInfo[rankData.type].roomId = rankData.roomId
    for k,v in pairs(rankData.rankList) do
        table.insert(rankSrvInfo[rankData.type], v)
    end
end

function DivineInspirationManager.GetRankSrvInfo(type)
    return rankSrvInfo[type] or {}
end

function DivineInspirationManager.CheckShowRed(label)
    local taskList = this.GetTaskList(label)
    if taskList then
        for k,v in pairs(taskList) do
            if v.cfgData and tonumber(tostring(v.srvData.process)) >= tonumber(v.cfgData.f_Progress) and (not v.srvData.rewarded) then
                return true
            end
        end
        if not label then
            local boxCfgData = this.GetBoxCfgData()
            local taskScore = this.GetMyTaskScore()
            for k,v in pairs(boxCfgData) do
                if taskScore >= v.f_tasknumber and not table.contains(this.boxRewardList, v.f_id) then
                    return true
                end
            end
        end
    end
    return false
end

function DivineInspirationManager.GetTableEleCount(tab)
    if tab then
        local count = 0
        for k,v in pairs(tab) do
            count = count + 1
        end
        return count
    else
        return 0
    end
end


function DivineInspirationManager.OnDestroy()
    taskCfg = nil
    costCfg = nil
    rankCfg = nil
    lableCfg = nil
end

return DivineInspirationManager